//
//  AcknowledgementViewController.m
//  bbqpitbooking
//
//  Created by Win Kyi Tin on 5/21/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "AcknowledgementViewController.h"
#import "HomeViewController.h"
#import "ViewATMListController.h"
#import "OnlinePayment.h";



@implementation AcknowledgementViewController


@synthesize btnBackToMain;
@synthesize btnFindATM;
@synthesize btnAddCalander;
@synthesize bookingDate;



//*******************************************************************************************************************

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Booking Successful";
	self.navigationItem.hidesBackButton = TRUE;
	
	NSLog(@"BookingDate in Ack: %@",self.bookingDate);
	
	
	// send email to Customer Gmail account.
/*	MFMailComposeViewController *picker =[[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	if ([MFMailComposeViewController canSendMail])
	{
		[picker setToRecipients:[NSArray arrayWithObjects:nil]];
		[picker setSubject:@"BBQ Pit Booking"]; 
		[picker setMessageBody:@"Your BBQ Pit Booking is successful." isHTML:NO]; 
		[self presentModalViewController:picker animated:YES]; 
		
	}
	[picker release];  */
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
	//return YES;
}

//*******************************************************************************************************************

/* - (void)mailComposeController:(MFMailComposeViewController*)controller
		  didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
	[controller dismissModalViewControllerAnimated:YES];
	
	
}*/

//*******************************************************************************************************************
//   Back To Main Screen

- (IBAction) BackToMain
{
	
	[self.navigationController popToRootViewControllerAnimated:YES];
}



//*******************************************************************************************************************

//  Show ATM Machine Place

- (IBAction) showATMLocation:(id) sender {
	
	 ViewATMListController *aTMListViewController = [[ViewATMListController alloc]
													 initWithNibName:@"ViewATMListController"
													 bundle:nil];
	[self.navigationController pushViewController:aTMListViewController animated:YES];
	[aTMListViewController release];
	
	
}


//*******************************************************************************************************************


// Sending SMS To Invite the event
-(IBAction)btnSend:(id)sender
{
	
	MFMessageComposeViewController *picker =[[MFMessageComposeViewController alloc] init] ;
    
	if ([MFMessageComposeViewController canSendText])
	{
		[picker setBody:@"Hi Buddy, Please join our BBQ party."]; 
		picker.recipients = [NSArray arrayWithObject:nil];
		picker.messageComposeDelegate = self;
		[self presentModalViewController:picker animated:YES];
	}	
	
	//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://91086072" ]];
	
	[picker release]; 
	

}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller 
				 didFinishWithResult:(MessageComposeResult)result {
	[controller	dismissModalViewControllerAnimated:YES];
}



//*******************************************************************************************************************

//   Add Event To Calander
	 
- (IBAction) addEventToCalander:(id) sender{
	
	
	[super viewDidLoad];
	
    EKEventStore *eventStore = [[EKEventStore alloc] init] ;
	
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = @"BBQ Event";
	event.location =@"Singapore";
	
	//event.startDate = self.bookingDate;
//	event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate]; 
	
	
	//Second Test
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
	[dateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
	[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Singapore/Singapore"]];
	
	NSString *startString = [NSString stringWithFormat:@"2011-06-07T17:00:00+11:00"];
	NSDate *dateStart = [dateFormatter dateFromString:startString];
	
	
	NSString *endString = [NSString stringWithFormat:@"2011-06-07T17:00:00+11:00"];
	NSDate *dateEnd = [dateFormatter dateFromString:endString];
	
	[dateFormatter release];
	event.startDate =dateStart;
	event.endDate = dateEnd;

	
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
	
	
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];  
	
	
	[eventStore release];
	if (err == noErr) {
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"Event"
							  message:@"BBQ Event is added in calendar"
							  delegate:nil
							  cancelButtonTitle:@"Okay"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}

	
	self.btnAddCalander.enabled =NO;	

	// add Notification 
			
	// get the date the user entered into the text field and save it iin a NSString
	NSString *dateString = [NSString stringWithFormat:@"2011-06-07T02:55:00+11:00"];
	
	// get the text the user entered into the text field and save it iin a NSString
	NSString *textString = @"Hay, Today is BBQ Party Day!!!";
	
	// create a NSDateFormatter that we will use to create a NSDate from the string
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
	[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Singapore/Singapore"]];	
	
	
	
	// create a NSDate from the string using our formatter
	NSDate *alertTime = [formatter dateFromString:dateString];
	
	[formatter release];
	// get an instance of our UIApplication
	UIApplication* app = [UIApplication sharedApplication] ;
	
    // create the notification and then set it's parameters
	UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
    if (notification)
    {
        notification.fireDate = alertTime;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.repeatInterval = 0;
		notification.alertBody = textString;
		
		notification.soundName=@"default";
		
		// this will schedule the notification to fire at the fire date
		[app scheduleLocalNotification:notification];
		
		// this will fire the notification right away, it will still also fire at the date we set
		//[app presentLocalNotificationNow:notification];
    }	
	
	
	
	
}


//*******************************************************************************************************************

//  Pay Online
- (IBAction) btnPayOnline:(id)sender {
	
	OnlinePayment *onlinePayment = [[OnlinePayment alloc]
													initWithNibName:@"OnlinePayment"
													bundle:nil];
	[self.navigationController pushViewController:onlinePayment animated:YES];
	[onlinePayment release];
}


//*******************************************************************************************************************

@end
